package com.lq.educationsystem.socre.service.impl;



import com.lq.educationsystem.common.util.*;
import com.lq.educationsystem.common.vo.ScorePageVo;
import com.lq.educationsystem.common.vo.SubjectStudentScoreVo;
import com.lq.educationsystem.socre.entiy.ScoreTb;
import com.lq.educationsystem.socre.mapper.ScoreTbMapper;
import com.lq.educationsystem.socre.service.ScoreBaseService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ScoreBaseImpl implements ScoreBaseService {

    //插入数据
    @Autowired
    ScoreTbMapper scoreTbMapper;


    @Override
    public void insertInfo(ScoreTb scoreInofo) {
        int row = scoreTbMapper.insert(scoreInofo);

        System.out.println("分数插入影响行数: "+row);
    }

    @Override
    public void deleteById(Integer scoreId) {
        int row = scoreTbMapper.deleteByPrimaryKey(Long.valueOf(scoreId));

        System.out.println("分数删除影响行数: "+row);
    }

    @Override
    public void updateInfo(ScoreTb scoreInofo) {
        int row = scoreTbMapper.updateByPrimaryKeySelective(scoreInofo);

        System.out.println("分数更新影响行数: "+row);
    }

    @Override
    public Result queryById(Integer scoreId) {

        ScoreTb scoreTb = scoreTbMapper.selectByPrimaryKey(Long.valueOf(scoreId));

        System.out.println("分数查询数据: "+scoreTb);
        return ResultBuild.buildOk(new Result<ScoreTb>(),scoreTb);
    }

    @Override
    public ResultPage<ScoreTb> queryList(ScorePageVo scorePageVo) {
        //获取分页信息
        Integer page = scorePageVo.getPage();
        Integer pageSize = scorePageVo.getPageSize();

        int start = PageUtil.getStart(page, pageSize);


        //获取数据
        ScoreTb scoreTb = new ScoreTb();
        BeanUtils.copyProperties(scorePageVo,scoreTb);

        //查询数据
        List<ScoreTb> scoreTbList = scoreTbMapper.queryList(start,pageSize,scoreTb);
        int counts=0;


        counts=scoreTbMapper.queryCount(scoreTb);
        return ResultPageBuild.pageBuildOk(new ResultPage<ScoreTb>(),page,pageSize,counts,scoreTbList);
    }

    @Override
    public ResultPage<ScoreTb> queryStudentScoreBySubjectIdAndStudentId(List<Integer> subjectIdList, Integer studentId) {

        List<ScoreTb> scoreInfoList = scoreTbMapper.queryStudentScoreBySubjectIdAndStudentId(subjectIdList,studentId);
        ResultPage<ScoreTb> scoreTbResultPage = new ResultPage<>();
        scoreTbResultPage.setData(scoreInfoList);
        return scoreTbResultPage;
    }

    /**
     * //通过学生id集合和课程id查询当前科目内所有学生的分数
     * @param studentIdInfoList
     * @param subjectId
     * @return
     */
    @Override
    public ResultPage<ScoreTb> querySubjectStudentScoreByStudentId(List<Integer> studentIdInfoList, Integer subjectId) {
        System.out.println("studentIdInfoList = " + studentIdInfoList + ", subjectId = " + subjectId);
        List<ScoreTb> scoreTbList = scoreTbMapper.querySubjectStudentScoreByStudentId(studentIdInfoList,subjectId);
        System.out.println("scoreTbList = " + scoreTbList);
        ResultPage<ScoreTb> scoreTbResultPage = new ResultPage<>();
        scoreTbResultPage.setData(scoreTbList);
        return scoreTbResultPage;
    }

    @Override
    public Result<ScoreTb> addSubjectStudentScoreByStudentIdAndSubjectId(List<Integer> studentIdInfoList, Integer subjectId) {
        //批量添加数据
        int row = scoreTbMapper.addSubjectStudentScoreByStudentIdAndSubjectId(studentIdInfoList,subjectId);
        Result<ScoreTb> scoreTbResultPage = new Result<>();

        return scoreTbResultPage;
    }

    @Override
    public Result updateStudentScoreByStudentIdAndSubjectId(SubjectStudentScoreVo subjectStudentScoreVo) {
        //插入数据
        ScoreTb scoreTb = new ScoreTb();
        scoreTb.setScore(subjectStudentScoreVo.getScore());
        scoreTb.setStudentId(subjectStudentScoreVo.getStudentId());
        scoreTb.setSubjectId(subjectStudentScoreVo.getSubjectId());
        int row = scoreTbMapper.updateStudentScoreByStudentIdAndSubjectId(scoreTb);

        if (row > 0) {
            return ResultBuild.buildOk();
        }else {
            return ResultBuild.buildDefeat();
        }


    }
}
